﻿function loadEvent(success)
{
    document.getElementById("event_area").innerHTML = '<div id="loading"><div id="loading-center-absolute"><div class="object w3-text-teal" id="object_four"></div><div class="object w3-text-teal" id="object_three"></div><div class="object w3-text-teal" id="object_two"></div><div class="object w3-text-teal" id="object_one"></div></div></div>';
    var xhttp;
    xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function () {
        if (xhttp.readyState == 4 && xhttp.status == 200) {
            document.getElementById("event_area").innerHTML = xhttp.responseText;
            if (success != "") {
                document.getElementById("success_content").innerHTML = success;
                document.getElementById("success-modal").style.display = "block";
                setTimeout('$("#success-modal").fadeOut()', 3000);
            }
        }
    };
    xhttp.open("GET", "../ajax/asp/events/load_event.aspx", true);
    xhttp.send();
}

function add_question(id) {
    flyToElement($("#" + id), $('#tbl_added_question_form'));
    var text = '<tr id="' + id + '" class="game_added w3-row ui-state-default ui-sortable-handle">' + document.getElementById(id).innerHTML + '<td class="w3-col l1"><i onclick="cancel_add_question(this.parentElement.parentElement.id)" class="fa fa-trash"></i></td></tr>';
    $("#" + id).remove();
    setTimeout(function () { document.getElementById("tbl_added_question_form").innerHTML += text }, 500);
    $(function () {
        $("#tbl_added_question_form").sortable();
        $("#tbl_added_question_form").disableSelection();
    });
}

function edit_show_question(id,event) {
    var count = document.getElementById("edit_player_added").getElementsByTagName("tr").length;
    var time = document.getElementById("edit_add_games_" + id).getElementsByClassName("edit_games_content_time")[0].innerHTML;
    var game_added = document.getElementById("edit_add_games_" + id).getElementsByClassName("edit_games_content_id")[0].innerHTML;
    var scores = document.getElementById("edit_add_games_" + id).getElementsByClassName("edit_games_content_score")[0].innerHTML.trim();
    count--;
    document.getElementById("add_game_modal").style.display = "block";
    document.getElementById("add_game_modal").innerHTML = '<div id="loading"><div id="loading-center-absolute"><div class="object w3-text-teal" id="object_four"></div><div class="object w3-text-teal" id="object_three"></div><div class="object w3-text-teal" id="object_two"></div><div class="object w3-text-teal" id="object_one"></div></div></div>';
    var xhttp;
    xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function () {
        if (xhttp.readyState == 4 && xhttp.status == 200) {
            document.getElementById("add_game_modal").innerHTML = xhttp.responseText;
            $(function () {
                $("#tbl_added_question_form").sortable();
                $("#tbl_added_question_form").disableSelection();
            });
        }
    };
    xhttp.open("GET", "../ajax/asp/Events/edit_games.aspx?id=" + id + "&player=" + count +"&added="+game_added +"&event="+event +"&time="+time+"&scores="+scores, true);
    xhttp.send();
}
function cancel_add_question(id) {
    flyToElement($("#" + id), $('#tbl_add_question_form'));
    var text = '<tr onclick="add_question(this.id)" id="' + id + '"><td class="w3-hide">' + document.getElementById(id).getElementsByTagName("td")[0].innerHTML+'</td>' +'<td class="w3-col l11">'+ document.getElementById(id).getElementsByTagName("td")[1].innerHTML + '</tr>'
    $("#" + id).remove();
    setTimeout(function () { document.getElementById("tbl_add_question_form").innerHTML += text }, 500);
    $(function () {
        $("#tbl_added_question_form").sortable();
        $("#tbl_added_question_form").disableSelection();
    });
}
function add_player(id) {
    flyToElement($("#" + id), $('#tbl_added_player_form'));
    var text = '<tr id="' + id + '" class="w3-row chosen-player">' + document.getElementById(id).innerHTML + '<td class="w3-col l1"><i onclick="cancel_add_player(this.parentElement.parentElement.id)" class="fa fa-trash"></i></td></tr>';
    $("#" + id).remove();
    setTimeout(function () { document.getElementById("tbl_added_player_form").innerHTML += text }, 500);
}
function cancel_add_player(id) {
    flyToElement($("#" + id), $('#tbl_add_player_form'));
    var text = '<tr id="' + id + '" onclick="add_player(this.id)" class="w3-row"><td style="padding: 0.8% 0.5%">' + document.getElementById(id).getElementsByTagName("td")[1].innerHTML + '</td><td style="padding: 0.8% 0.5%">' + document.getElementById(id).getElementsByTagName("td")[2].innerHTML + '</td><td style="padding: 0.8% 0.5%">' + document.getElementById(id).getElementsByTagName("td")[3].innerHTML + '</td></tr>';
    $("#" + id).remove();
    setTimeout(function () { document.getElementById("tbl_add_player_form").innerHTML += text }, 500);
}
function event_edit(id) {
    document.getElementById("event-edit-form").style.display = "block";
    document.getElementById("event-edit-form").innerHTML = '<div id="loading"><div id="loading-center-absolute"><div class="object w3-text-teal" id="object_four"></div><div class="object w3-text-teal" id="object_three"></div><div class="object w3-text-teal" id="object_two"></div><div class="object w3-text-teal" id="object_one"></div></div></div>';
    var xhttp;
    xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function () {
        if (xhttp.readyState == 4 && xhttp.status == 200) {
            document.getElementById("event-edit-form").innerHTML = xhttp.responseText;
            $(function () {
                $("#sortable_add_game").sortable();
                $("#sortable_add_game").disableSelection();
            });
        }
    };
    xhttp.open("GET", "../ajax/asp/Events/edit_event.aspx?id=" + id, true);
    xhttp.send();
}
function btn_add_player() {
    var player = document.getElementById("player_added_hidden").innerHTML.trim();
    document.getElementById('add_player_modal').style.display = 'block';
    document.getElementById("add_player_modal").innerHTML = '<div id="loading"><div id="loading-center-absolute"><div class="object w3-text-teal" id="object_four"></div><div class="object w3-text-teal" id="object_three"></div><div class="object w3-text-teal" id="object_two"></div><div class="object w3-text-teal" id="object_one"></div></div></div>';
    var xhttp;
    xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function () {
        if (xhttp.readyState == 4 && xhttp.status == 200) {
            document.getElementById("add_player_modal").innerHTML = xhttp.responseText;
        }
    };
    xhttp.open("GET", "../ajax/asp/Events/add_player.aspx?player=" + player, true);
    xhttp.send();
}
function load_table_player_added(chosen) {
    document.getElementById("edit_player_added").innerHTML = '<div id="loading"><div id="loading-center-absolute"><div class="object w3-text-teal" id="object_four"></div><div class="object w3-text-teal" id="object_three"></div><div class="object w3-text-teal" id="object_two"></div><div class="object w3-text-teal" id="object_one"></div></div></div>';
    var xhttp;
    xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function () {
        if (xhttp.readyState == 4 && xhttp.status == 200) {
            document.getElementById("edit_player_added").innerHTML = xhttp.responseText;
        }
    };
    xhttp.open("GET", "../ajax/asp/Events/load_table_player_added.aspx?player=" + chosen, true);
    xhttp.send();
}
function btn_add_chosen_player() {
    var chosen = "";
    var chosen_player = document.getElementsByClassName("chosen-player");
    if (chosen_player.length) {
        for (var i = 0; i < chosen_player.length; i++) {
            chosen += chosen_player[i].getElementsByTagName("td")[0].innerHTML;
            chosen += ",";
        }
    }
    document.getElementById("player_added_hidden").innerHTML = chosen;
    document.getElementById('add_player_modal').style.display = 'none';
    load_table_player_added(chosen);
}

function btn_add_game(id)
{
    var game_added = "";
    var time = document.getElementById("add_game_modal").getElementsByClassName("edit_game_time")[0].value;
    var score = "";
    if ($("#add_game_modal .game_added").length)
    {
        for (var i = 0; i < document.getElementById("add_game_modal").getElementsByClassName("game_added").length; i++)
        {
            game_added += document.getElementById("add_game_modal").getElementsByClassName("game_added")[i].getElementsByTagName("td")[0].innerHTML + ",";
        }
    }
    if ($("#add_game_modal .edit_game_score").length) {
        for (var i = 0; i < document.getElementById("add_game_modal").getElementsByClassName("edit_game_score").length; i++) {
            score += document.getElementById("add_game_modal").getElementsByClassName("edit_game_score")[i].value + ",";
        }
    }
    document.getElementById("edit_add_games_" + id).getElementsByClassName("edit_games_content_id")[0].innerHTML = game_added;
    document.getElementById("edit_add_games_" + id).getElementsByClassName("edit_games_content_time")[0].innerHTML = time;
    document.getElementById("edit_add_games_" + id).getElementsByClassName("edit_games_content_score")[0].innerHTML = score;
    document.getElementById("add_game_modal").style.display="none";
}

function btn_edit_event(id)
{
    var name = document.getElementById("edit_event_name").value;
    var player = document.getElementById("player_added_hidden").innerHTML.trim();
    var games = "";
    var game_l = document.getElementById("sortable_add_game").getElementsByTagName("tr");
    for(var i=0;i<game_l.length;i++)
    {
        var temp = "";
        for(var j=0;j<game_l[i].getElementsByTagName("td").length-1;j++)
        {
            temp += game_l[i].getElementsByTagName("td")[j].innerHTML.trim();
            temp += "_";
        }
        games += temp;
        games += ";";
    }

    var xhttp;
    xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function () {
        if (xhttp.readyState == 4 && xhttp.status == 200) {
            document.getElementById("event_area").innerHTML = xhttp.responseText;
            loadEvent("Chỉnh sửa đã được lưu!");
        }
    };
    xhttp.open("GET", "../ajax/asp/events/edited_event.aspx?id="+id+"&name="+name+"&player="+player+"&games="+games, true);
    xhttp.send();
}

function btn_change_name(id,name)
{
    document.getElementById("event_change_name_player_modal").style.display = "block";
    document.getElementById("change_player_name_id").innerHTML = id;
    document.getElementById("change_player_name").value = name;
}

function btn_accept_change_name()
{
    var id = document.getElementById("change_player_name_id").innerHTML;
    var name = document.getElementById("change_player_name").value;

    if(name.trim().length<2 || name.trim() =="")
    {
        document.getElementById("error_content").innerHTML = "Tên người chơi quá ngắn!";
        document.getElementById("error-modal").style.display = "block";
        setTimeout('$("#error-modal").fadeOut()', 3000);
    }
    else
    {
        var xhttp;
        xhttp = new XMLHttpRequest();
        xhttp.onreadystatechange = function () {
            if (xhttp.readyState == 4 && xhttp.status == 200) {
                document.getElementById("success_content").innerHTML = "Đổi tên thành công";
                document.getElementById("success-modal").style.display = "block";
                setTimeout('$("#success-modal").fadeOut()', 3000);
                var chosen = document.getElementById("player_added_hidden").innerHTML;
                document.getElementById("event_change_name_player_modal").style.display = "none";
                load_table_player_added(chosen);
            }
        };
        xhttp.open("GET", "../ajax/asp/events/change_name_player.aspx?id=" + id + "&name=" + name, true);
        xhttp.send();
    }
}